package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SProtectDevice;
import com.ruoyi.system.domain.SSubstationDevice;
import com.ruoyi.system.service.ISSubstationDeviceService;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SDeviceVoltageLevelMapper;
import com.ruoyi.system.domain.SDeviceVoltageLevel;
import com.ruoyi.system.service.ISDeviceVoltageLevelService;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * 设备电压等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class SDeviceVoltageLevelServiceImpl extends ServiceImpl<SDeviceVoltageLevelMapper, SDeviceVoltageLevel> implements ISDeviceVoltageLevelService
{

    @Autowired
    private ISSubstationDeviceService substationDeviceService;

    @Override
    public boolean checkDeviceVoltageLevel(SDeviceVoltageLevel sDeviceVoltageLevel) {
        if(StringUtils.isBlank(sDeviceVoltageLevel.getDeviceVoltageLevel())){
            throw new ServiceException("电压等级名称不能为空！");
        }
        long count = this.count(new LambdaQueryWrapper<SDeviceVoltageLevel>()
                .eq(SDeviceVoltageLevel::getDeviceVoltageLevel, sDeviceVoltageLevel.getDeviceVoltageLevel()));
        if(count > 0){
            throw new ServiceException("电压等级：" + sDeviceVoltageLevel.getDeviceVoltageLevel() + " 已存在！");
        }
        return true;
    }

    @Override
    public String importData(MultipartFile file) throws Exception{


        ExcelUtil<SSubstationDevice> util = new ExcelUtil<SSubstationDevice>(SSubstationDevice.class);
        Sheet sheet = util.getSheet("", file.getInputStream());
        int lastRowNum = sheet.getLastRowNum();
        if(lastRowNum <= 0) return "数据为空！";

        // 创建变电站设备对象，用来保存表格中的前4行的值
        SSubstationDevice substationDevice = substationDeviceService.createSubstationDevice(sheet);
        // 设置设备电压等级
        if(StringUtils.isNotBlank(substationDevice.getDeviceName())) {
            String deviceName = substationDevice.getDeviceName();
            String kV = deviceName.split("kV")[0];
            if(StringUtils.isNotBlank(kV)){
                kV = kV + "kV";
                SDeviceVoltageLevel deviceVoltageLevel = this.getOne(new LambdaQueryWrapper<SDeviceVoltageLevel>()
                        .eq(SDeviceVoltageLevel::getDeviceVoltageLevel, kV));
                // 不存在则新建
                if(StringUtils.isNull(deviceVoltageLevel)){
                    deviceVoltageLevel = new SDeviceVoltageLevel();
                    deviceVoltageLevel.setDeviceVoltageLevel(kV);
                    this.save(deviceVoltageLevel);
                }
            }
        }
        return "导入成功！";
    }
}
